﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace System.MVC.Filters
{
    public class SystemAuthAttrbute:AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {

            //未登录不可直接访问其他页面
            if (!(filterContext.HttpContext.Session["UserName"] != null || filterContext.HttpContext.Request.Cookies["UserName"] != null))
            {
                filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary()
                {
                    {"controller","Home" },
                    {"action","Login" }
                });
            }


            else if (filterContext.HttpContext.Session["UserName"] == null && filterContext.HttpContext.Request.Cookies["UserName"] != null)
            {//System.Web.HttpContext.Current.Server.UrlDecode(Request.Cookies["name"].Value)
                filterContext.HttpContext.Session["UserName"] = System.Web.HttpContext.Current.Server.UrlDecode(filterContext.HttpContext.Request.Cookies["UserName"].Value);
                filterContext.HttpContext.Session["userid"] = filterContext.HttpContext.Request.Cookies["userid"].Value;
            }



        }
    }
}